package cn.nawang.ebeim.client.bean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.nawang.ebeim.client.util.CommonMethodsUtils;
import cn.nawang.ebeim.client.util.Constants;
import cn.nawang.ebeim.client.util.FileUtil;
import cn.nawang.ebeim.client.util.PropertiesConstants;
import cn.nawang.ebeim.client.version.FileNode;

/**
 * @ClassName: LocalBackupTask
 * @Description: 本地备份任务
 * @author 张建峰
 * @date 2015年9月17日 上午11:41:24
 * @version v1.0
 */
public class LocalBackupTask {
  
  private static final Logger LOG = LoggerFactory.getLogger(LocalBackupTask.class);
  
  private String root;
  private String dsId;
  private FileNode fileNode;
  private String version;
  
  public String getRoot() {
    return root;
  }
  public void setRoot(String root) {
    this.root = root;
  }
  public String getDsId() {
    return dsId;
  }
  public void setDsId(String dsId) {
    this.dsId = dsId;
  }
  public FileNode getFileNode() {
    return fileNode;
  }
  public void setFileNode(FileNode fileNode) {
    this.fileNode = fileNode;
  }
  
  public LocalBackupTask(String root,String dsId, FileNode fileNode,String version) {
    super();
    this.dsId = dsId;
    this.fileNode = fileNode;
    this.root = root;
    this.version = version;
  }
  
  private static final long SPACE_PADDING = 1024 * 1024 * 50;
  
  public void execute(){
    File disk = new File(getDiskName());  
    long freeSpace = disk.getFreeSpace();
    
    LOG.info("localBackup choose disk...."+disk+":freeSpace:"+disk.getFreeSpace());
    
    String localBackupPath = ClientConfiguration.getLocalBackupPath();
    FileUtil.choosePath(localBackupPath,PropertiesConstants.LOCAL_BACKUP_PATH,FileUtil.PATH_BACKUP_LOCAL);
    
    String path = root + File.separator + fileNode.getPath() +File.separator+ fileNode.getName();
    String dsIdPath = ClientConfiguration.getLocalBackupPath() + File.separator + dsId;
    String backupPath = dsIdPath + File.separator + version + File.separator
        + fileNode.getPath() + File.separator + fileNode.getName();
    
    File backUpFile = new File(backupPath);
    //如果可用空间小于 50M 或 即将备份的文件长度 大于 可用空间-50M
    if(freeSpace < SPACE_PADDING || backUpFile.length() > freeSpace - SPACE_PADDING){
      LOG.error("space is not enough!");
      return;
    }
    
    File file = new File(dsIdPath);
    if(!file.exists()){
      file.mkdirs();
    }
    
    //遍历已版本号命名的文件夹
    File[] files = file.listFiles();
    List<Float> versionNames = new ArrayList<Float>();
    for (File f : files) {
      if(CommonMethodsUtils.isNum(f.getName())){
        versionNames.add(Float.valueOf(f.getName()));
      }
    }
    versionNames = CommonMethodsUtils.sort(versionNames);
    
    Integer localBackupMax = ClientConfiguration.getLocalBackupMax();
    //备份的版本数量小于本地最大备份数量
    if(versionNames.size() < (localBackupMax==null?Constants.LOCAL_BACKUP＿MAX_DEFAULT:localBackupMax)){
      
      CommonMethodsUtils.copyFile(path, backupPath);
      
    }else if(versionNames.size() == ClientConfiguration.getLocalBackupMax()){
      boolean exist = false;
      for (File f : files) {
        if(f.getName().equals(version)){
          exist = true;
        }
      }
      if(!exist){
        CommonMethodsUtils.deleteFolder(dsIdPath + File.separator + versionNames.get(0));
      }
      CommonMethodsUtils.copyFile(path, backupPath);
    }
    
  }
  
  public String getDiskName(){
    String diskName="";
    String localBackupPath = ClientConfiguration.getLocalBackupPath();
    if(localBackupPath!=null&&!localBackupPath.equals("")){
      diskName = localBackupPath.substring(0, localBackupPath.indexOf(File.separator));
    } 
    return diskName;
  }
  
  
}
